package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.GetGoodsAppLogForm;
import com.woniuxy.controller.form.SaveGoodsAppraiseLogForm;
import com.woniuxy.controller.info.GoodsAppLogInfo;
import com.woniuxy.dao.AccountDao;
import com.woniuxy.dao.PawnGoodsDao;
import com.woniuxy.model.Account;
import com.woniuxy.model.PawnGoods;
import com.woniuxy.model.PawnGoodsAppraiseLog;
import com.woniuxy.dao.PawnGoodsAppraiseLogDao;
import com.woniuxy.service.PawnGoodsAppraiseLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnGoodsAppraiseLogServiceImpl extends ServiceImpl<PawnGoodsAppraiseLogDao, PawnGoodsAppraiseLog> implements PawnGoodsAppraiseLogService {

    @Resource
    private AccountDao ad;

    @Resource
    private PawnGoodsDao pgd;
    /**
     * 根据商品id查询历史评估记录
     * @param ggalf goodsCode
     * @return GoodsAppLogInfo
     */
    @Override
    public Result getGoodsAppraiseLogByGoodsId(GetGoodsAppLogForm ggalf) {
        QueryWrapper<PawnGoodsAppraiseLog> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", ggalf.getGoodsId());
        Page<PawnGoodsAppraiseLog> getGoodsAppLogForm = baseMapper.selectPage(ggalf, wrapper);
        Page<GoodsAppLogInfo> goodsAppLogInfoPage = new Page<>();
        List<GoodsAppLogInfo> goodsAppLogInfos = NewCopyBean.copyList(getGoodsAppLogForm.getRecords(), GoodsAppLogInfo.class);
        goodsAppLogInfoPage.setRecords(goodsAppLogInfos);
        goodsAppLogInfoPage.setTotal(getGoodsAppLogForm.getTotal());
        goodsAppLogInfoPage.setSize(getGoodsAppLogForm.getSize());
        goodsAppLogInfoPage.setCurrent(getGoodsAppLogForm.getCurrent());
        return Result.success(200,"请求成功",goodsAppLogInfoPage);
    }

    /**
     * 新建评估
     * @param sgalf SaveGoodsAppraiseLogForm
     * @return data为null
     */
    @Override
    @Transactional
    public Result saveGoodsAppraiseLog(SaveGoodsAppraiseLogForm sgalf) {
        QueryWrapper<PawnGoodsAppraiseLog> wrapper = new QueryWrapper<>();
        wrapper.eq("appraise_stat", 1);
        wrapper.eq("goods_id",sgalf.getGoodsId());
        PawnGoodsAppraiseLog appraise_stat = baseMapper.selectOne(wrapper);
        if(ObjectUtils.isEmpty(appraise_stat)){
            PawnGoodsAppraiseLog pawnGoodsAppraiseLog = NewCopyBean.copyOne(sgalf, PawnGoodsAppraiseLog.class);
            pawnGoodsAppraiseLog.setCreateTime(new Date());
            pawnGoodsAppraiseLog.setAppraiseStat(1);
            Account account = ad.selectById(sgalf.getAccountId());
            pawnGoodsAppraiseLog.setCreateBy(account.getAccountUsername());
            baseMapper.insert(pawnGoodsAppraiseLog);
        }else{
            appraise_stat.setAppraiseStat(0);
            baseMapper.updateById(appraise_stat);
            PawnGoodsAppraiseLog pawnGoodsAppraiseLog = NewCopyBean.copyOne(sgalf, PawnGoodsAppraiseLog.class);
            pawnGoodsAppraiseLog.setCreateTime(new Date());
            pawnGoodsAppraiseLog.setAppraiseStat(1);
            Account account = ad.selectById(sgalf.getAccountId());
            pawnGoodsAppraiseLog.setCreateBy(account.getAccountUsername());
            baseMapper.insert(pawnGoodsAppraiseLog);
        }
        PawnGoods pawnGoods = pgd.selectById(sgalf.getGoodsId());
        pawnGoods.setAssessor(sgalf.getAccountId());
        pgd.updateById(pawnGoods);
        return Result.success(200,"评估成功",null);
    }
}
